package servlet;

import model.Category;
import service.CategoryService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "admin_category_edit",urlPatterns = "/admin/category_edit")
public class AdminCategoryEditServlet extends HttpServlet {
    private CategoryService tService = new CategoryService();
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Category t = new Category();
        try {//将请求中的参数映射到category对象的属性中
            BeanUtils.copyProperties(t, request.getParameterMap());
            //获取is_use的值
            String isuseParam = request.getParameter("is_use");
            if(isuseParam!=null){
                boolean is_use = Boolean.parseBoolean(isuseParam);
                t.setIs_use(is_use);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tService.update(t);
        request.getRequestDispatcher("/admin/category_list").forward(request, response);
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
